﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Windows;
using System.Windows.Controls;
using System.Globalization;
using System.Collections.Generic;
using Microsoft.WVB.Silverlight.Infrastructure;
using Microsoft.WVB.Framework.DataServicesEntities;
using System.Windows.Data;

namespace Microsoft.WVB.UI
{
    public enum UpdateMode
    {
        All,
        Day,
        Month,
        Channel        
    }

    public partial class EpgChannelDateSelector : UserControl
    {
        public EPGModel ViewModel { get; set; }
        bool IsLoaded = false;

        public int DaySelectorIndex
        {
            get { return (int)GetValue(DaySelectorIndexProperty); }
            set { SetValue(DaySelectorIndexProperty, value); }
        }
        public static readonly DependencyProperty DaySelectorIndexProperty = null;

        public int MonthSelectorIndex
        {
            get { return (int)GetValue(MonthSelectorIndexProperty); }
            set { SetValue(MonthSelectorIndexProperty, value); }
        }
        public static readonly DependencyProperty MonthSelectorIndexProperty = null;

        public Channel ChannelSelector
        {
            get { return (Channel)GetValue(ChannelSelectorProperty); }
            set { SetValue(ChannelSelectorProperty, value); }
        }
        public static readonly DependencyProperty ChannelSelectorProperty = null;

        static EpgChannelDateSelector()
        {
            DaySelectorIndexProperty = DependencyProperty.Register("DaySelectorIndex", typeof(int), typeof(EpgChannelDateSelector), new PropertyMetadata(new PropertyChangedCallback(OnDaySelectorIndexChanged)));
            MonthSelectorIndexProperty = DependencyProperty.Register("MonthSelectorIndex", typeof(int), typeof(EpgChannelDateSelector), new PropertyMetadata(new PropertyChangedCallback(OnMonthSelectorIndexChanged)));
            ChannelSelectorProperty = DependencyProperty.Register("ChannelSelector", typeof(Channel), typeof(EpgChannelDateSelector), new PropertyMetadata(new PropertyChangedCallback(OnChannelSelectorChanged)));
        }

        public EpgChannelDateSelector()
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                ViewModel = IoC.GetCreate<EPGModel>();
                DataContext = ViewModel;
            }

            this.Loaded += new RoutedEventHandler(EpgChannelDateSelector_Loaded);
            InitializeComponent();

            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                Binding day = new Binding("SelectedDay");
                day.Mode = BindingMode.OneWay;
                day.Source = ViewModel;
                this.SetBinding(EpgChannelDateSelector.DaySelectorIndexProperty, day);

                Binding month = new Binding("SelectedMonth");
                month.Mode = BindingMode.OneWay;
                month.Source = ViewModel;
                this.SetBinding(EpgChannelDateSelector.MonthSelectorIndexProperty, month);

                Binding ch = new Binding("SelectedChannel");
                ch.Mode = BindingMode.OneWay;
                ch.Source = ViewModel;
                this.SetBinding(EpgChannelDateSelector.ChannelSelectorProperty, ch);
            }
        }

        private static void OnDaySelectorIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
                ((EpgChannelDateSelector)d).UpdateView(UpdateMode.Day);
        }

        private static void OnMonthSelectorIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
                ((EpgChannelDateSelector)d).UpdateView(UpdateMode.Month);
        }

        private static void OnChannelSelectorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((EpgChannelDateSelector)d).UpdateView(UpdateMode.All);
        }

        private void UpdateView(UpdateMode mode)
        {
            if ((this.IsLoaded) && (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)))
            {
                switch (mode)
                {
                    case UpdateMode.All:
                        {
                            if (DayPicker.SelectedIndex != ViewModel.SelectedDay)
                                DayPicker.SelectedIndex = ViewModel.SelectedDay;

                            if (MonthPicker.SelectedIndex != ViewModel.SelectedMonth)
                                MonthPicker.SelectedIndex = ViewModel.SelectedMonth;
                        }
                        break;

                    case UpdateMode.Day:
                        {
                            if (DayPicker.SelectedIndex != ViewModel.SelectedDay)
                                DayPicker.SelectedIndex = ViewModel.SelectedDay;
                        }
                        break;

                    case UpdateMode.Month:
                        {
                            if (MonthPicker.SelectedIndex != ViewModel.SelectedMonth)
                                MonthPicker.SelectedIndex = ViewModel.SelectedMonth;
                        }
                        break;

                    case UpdateMode.Channel:
                        break;
                    default:
                        break;
                }                
            }
        }

        void EpgChannelDateSelector_Loaded(object sender, RoutedEventArgs e)
        {
            this.IsLoaded = true;

            DayPicker.SelectionChanged += new SelectionChangedEventHandler(DayPicker_SelectionChanged);
            MonthPicker.SelectionChanged += new SelectionChangedEventHandler(MonthPicker_SelectionChanged);
            ChannelPicker.SelectionChanged += new SelectionChangedEventHandler(ChannelPicker_SelectionChanged);
            
            UpdateView(UpdateMode.All);
        }

        void ChannelPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ChannelPicker.SelectedItem != null)
            {
                ViewModel.SetSelectedChannel((Channel)e.AddedItems[0]);
            }
        }

        void MonthPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (MonthPicker.SelectedItem != null)
            {
                ViewModel.SetSelectedMonth(MonthPicker.SelectedIndex);
            }
        }

        void DayPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (DayPicker.SelectedItem != null)
            {
                ViewModel.SetSelectedDay(DayPicker.SelectedIndex);
            }
        }
    }
}
